/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.ThuePhong;

import BUS.LoaiPhong_BUS;
import BUS.Phong_BUS;
import DTO.LoaiPhong_DTO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public class JIFThemLoaiPhong extends javax.swing.JInternalFrame {
    DefaultTableModel modelLoaiPhong;

    /**
     * Creates new form JIFThemLoaiPhong
     */
    public JIFThemLoaiPhong() {
        initComponents();
        this.setSize(880, 560);
        showLoaiPhong();
        btCapNhatLoaiPhong.setEnabled(false);
        btXoaLoaiPhong.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel6 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfTenLoaiPhong = new javax.swing.JTextField();
        tfDonGia = new javax.swing.JTextField();
        btThemLoaiPhong = new javax.swing.JButton();
        btXoaLoaiPhong = new javax.swing.JButton();
        btCapNhatLoaiPhong = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbLoaiPhong = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("QUẢN LÝ LOẠI PHÒNG");

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel5.setText("Tên loại phòng: ");

        jLabel6.setText("Đơn giá:");

        btThemLoaiPhong.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btThemLoaiPhong.setText("Thêm loại phòng");
        btThemLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemLoaiPhongActionPerformed(evt);
            }
        });

        btXoaLoaiPhong.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Erase.png"))); // NOI18N
        btXoaLoaiPhong.setText("Xóa");
        btXoaLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaLoaiPhongActionPerformed(evt);
            }
        });

        btCapNhatLoaiPhong.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Sync.png"))); // NOI18N
        btCapNhatLoaiPhong.setText("Cập nhật");
        btCapNhatLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatLoaiPhongActionPerformed(evt);
            }
        });

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(btThemLoaiPhong)
                        .addGap(18, 18, 18)
                        .addComponent(btXoaLoaiPhong)
                        .addGap(18, 18, 18)
                        .addComponent(btCapNhatLoaiPhong)
                        .addGap(18, 18, 18)
                        .addComponent(btThoat))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfTenLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(tfTenLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(tfDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCapNhatLoaiPhong)
                    .addComponent(btXoaLoaiPhong)
                    .addComponent(btThemLoaiPhong)
                    .addComponent(btThoat))
                .addGap(20, 20, 20))
        );

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tbLoaiPhong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbLoaiPhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLoaiPhongMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbLoaiPhong);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btThemLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemLoaiPhongActionPerformed
        // TODO add your handling code here:
        int rows = tbLoaiPhong.getRowCount();
        float dongia;
        LoaiPhong_DTO p = new LoaiPhong_DTO();
        try {

            if (rows <= 0) {
                p.setMaloaiPhong(Phong_BUS.taoMaTuDong("MLP0000", 3));
            } else {
                p.setMaloaiPhong(Phong_BUS.taoMaTuDong(modelLoaiPhong.getValueAt(rows - 1, 0).toString(), 3));
            }
            if (tfTenLoaiPhong.getText().equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập tên loại phòng!");
                return;
            } else {
                p.setTenloaiPhong(tfTenLoaiPhong.getText());
            }

            try {
                dongia = Float.parseFloat(tfDonGia.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số!");
                return;
            }

            if (tfDonGia.getText().equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập đơn giá!");
                return;
            } else {
                p.setDonGia(Float.toString(dongia));
            }
            LoaiPhong_BUS.Insert(p);
            JOptionPane.showMessageDialog(rootPane, "Thêm loại phòng thành công!");

        } catch (Exception ex) {
            Logger.getLogger(JIFThemLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showLoaiPhong();
    }//GEN-LAST:event_btThemLoaiPhongActionPerformed

    private void btXoaLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaLoaiPhongActionPerformed
        // TODO add your handling code here:
        int row = this.tbLoaiPhong.getSelectedRow();
        int show = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?", "", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            try {
                String maloaiPhong = (String) this.modelLoaiPhong.getValueAt(row, 0).toString();
                LoaiPhong_BUS.Delete(maloaiPhong);
                JOptionPane.showMessageDialog(rootPane, "Xóa loại phòng thành công!");

            } catch (Exception e) {
            }
            showLoaiPhong();
        }
        else{
            return;
        }
    }//GEN-LAST:event_btXoaLoaiPhongActionPerformed

    private void btCapNhatLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatLoaiPhongActionPerformed
        // TODO add your handling code here:
        int row = tbLoaiPhong.getSelectedRow();
        float dongia;
        LoaiPhong_DTO p = new LoaiPhong_DTO();
        try {
            p.setMaloaiPhong(modelLoaiPhong.getValueAt(row, 0).toString());
            p.setTenloaiPhong(tfTenLoaiPhong.getText());
            try {
                dongia = Float.parseFloat(tfDonGia.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số!");
                return;
            }
            p.setDonGia(Float.toString(dongia));
            LoaiPhong_BUS.Update(p);
            JOptionPane.showMessageDialog(rootPane, "Cập nhật loại phòng thành công!");

        } catch (Exception ex) {
            Logger.getLogger(JIFThemLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showLoaiPhong();
    }//GEN-LAST:event_btCapNhatLoaiPhongActionPerformed

    private void tbLoaiPhongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLoaiPhongMouseClicked
        // TODO add your handling code here:
        int row = tbLoaiPhong.getSelectedRow();
        if (row >= 0) {
            btCapNhatLoaiPhong.setEnabled(true);
            btXoaLoaiPhong.setEnabled(true);
        }
        String tenloaiPhong = modelLoaiPhong.getValueAt(row, 1).toString();
        String dongia = modelLoaiPhong.getValueAt(row, 2).toString();
        tfTenLoaiPhong.setText(tenloaiPhong);
        tfDonGia.setText(dongia);
    }//GEN-LAST:event_tbLoaiPhongMouseClicked

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhatLoaiPhong;
    private javax.swing.JButton btThemLoaiPhong;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXoaLoaiPhong;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbLoaiPhong;
    private javax.swing.JTextField tfDonGia;
    private javax.swing.JTextField tfTenLoaiPhong;
    // End of variables declaration//GEN-END:variables

    private void showLoaiPhong() {
        try {
            modelLoaiPhong = LoaiPhong_BUS.layDanhSachLoaiPhong();
            tbLoaiPhong.setModel(modelLoaiPhong);
        } catch (Exception ex) {
            Logger.getLogger(JIFThemLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
